package application.visual;

import gnu.io.CommPortIdentifier;
import info.clearthought.layout.TableLayout;
import io.IOClient;
import io.SerialCommunicator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import api.AdeptInterpreter;
import api.Interpreter;
import api.MicrobotInterpreter;
import application.Launcher;

public class AddConnectionDialog extends JDialog{

	private JPanel mainPanel;
	private List<IOClient> clients;
	private List<Interpreter> interpreters;
	private JComboBox connCombo, intCombo;
	private JTextField variableInput;
	private JPanel buttonPanel;
	private JButton cancelButton, createButton;
	
	public AddConnectionDialog()
	{
		super((JFrame)null, true);
		initGUI();
	}
	private List<Interpreter> getInterpreters()
	{
		if(interpreters == null)
		{
			interpreters = new ArrayList<Interpreter>();
			interpreters.add(new AdeptInterpreter());
			interpreters.add(new MicrobotInterpreter());
		}
		return interpreters;
	}
	private List<IOClient> getClients()
	{
		if(clients == null)
		{
			clients = new ArrayList<IOClient>();
			
			Enumeration e = CommPortIdentifier.getPortIdentifiers();
			while(e.hasMoreElements())
				clients.add(SerialCommunicator.getCommunicator(((CommPortIdentifier)e.nextElement()).getName()));
			
			clients.removeAll(Launcher.getMainFrame().getConnectedClients());
		}
		
		return clients;
	}
	
	private JPanel getMainPanel()
	{
		if(mainPanel == null)
		{
			mainPanel = new JPanel();
			if(getClients().size() == 0)
			{
				TableLayout layout = new TableLayout(new double[][]{
						{5,290,5},{100, 25}
				});
				
				mainPanel.setLayout(layout);
				mainPanel.add(new JLabel("No Connections are Available"), "1,0,1,0,c,c,");
				mainPanel.add(new JButton("Ok"){
					{
						addActionListener(
							new ActionListener(){
								public void actionPerformed(ActionEvent e){
									AddConnectionDialog.this.dispose();
								}
						});
					}
				}, "1,1,1,1,r,c");
			}
			else
			{
				TableLayout layout = new TableLayout(new double[][]{
						{5,390,5},{5,20,20,20,20,20,20,30}
				});
				
				mainPanel.setLayout(layout);
				mainPanel.add(new JLabel("Connection"), "1,1,1,1,l,c");
				mainPanel.add(new JLabel("Variable Name"), "1,5,1,5,l,c");
				mainPanel.add(getConnectionCombo(), "1,2,1,2,f,f");
				mainPanel.add(getVariableInput(), "1,6,1,6,f,f");
				mainPanel.add(getButtonPanel(), "1,7,1,7, r, f");
				mainPanel.add(new JLabel("Interpreter Type"), "1,3,1,3,l,c");
				mainPanel.add(getInterpreterCombo(), "1,4,1,4,f,f");
			}
		}
		
		return mainPanel;
	}
	
	private JPanel getButtonPanel()
	{
		if(buttonPanel == null)
		{
			buttonPanel = new JPanel();
			TableLayout layout = new TableLayout(new double[][]{
					{.5,.5}, {30}
			});
			
			buttonPanel.setLayout(layout);
			
			buttonPanel.add(getCancelButton(), "0,0,0,0,r,c");
			buttonPanel.add(getCreateButton(), "1,0,1,0,l,c");
		}
		return buttonPanel;
	}
	private JComboBox getConnectionCombo()
	{
		if(connCombo == null)
		{
			connCombo = new JComboBox(getClients().toArray());
		}
		
		return connCombo;
	}
	private JComboBox getInterpreterCombo()
	{
		if(intCombo == null)
			intCombo = new JComboBox(getInterpreters().toArray());
		return intCombo;
	}
	private JButton getCancelButton()
	{
		if(cancelButton == null)
		{
			cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
					AddConnectionDialog.this.dispose();
				}
			});
		}
		
		return cancelButton;
	}
	
	private JButton getCreateButton(){
		if(createButton == null)
		{
			createButton = new JButton("Create");
			createButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					if(verifyInput())
					{
						Launcher.getMainFrame().addConnection(getVariableInput().getText().trim(), getConnectionCombo().getSelectedItem().toString(), (Interpreter)getInterpreterCombo().getSelectedItem());
						dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(AddConnectionDialog.this, "Invalid Variable Name");
					}
				}
				
				private boolean verifyInput()
				{
					return getVariableInput().getText().trim().length() > 0 && Launcher.getMainFrame().variableCheck(getVariableInput().getText().trim());
				}
			});
		}
		return createButton;
	}
	private JTextField getVariableInput()
	{
		if(variableInput == null)
		{
			variableInput = new JTextField();
		}
		
		return variableInput;
	}
	private void initGUI()
	{
		getContentPane().add(getMainPanel());
		pack();
	}
}
